<template>
  <el-dialog
    class="set_dialog"
    width="500px"
    title="批量复制"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
  >
    <p class="text-center tip">请<span style="color:red">慎重</span>选择添加商品的商户</p>
    <el-form
      ref="ruleForm"
      class="ruleForm"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item
        label="商户"
        prop="reason"
      >
        <el-select
          v-model="form.activity"
          placeholder="请选择"
          multiple
          collapse-tags
        >
          <el-option
            v-for="item in activityList"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        size="small"
        type="primary"
        :loading="isClick"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import { getActivityList, addActivity } from '@/api/audit'
import { getSupplierList } from '@/api/user'
import { batchCopyGoods } from '@/api/recommend'
export default {
  props: [
    'goods'
  ],
  data() {
    return {
      form: {
        activity: ''
      },
      rules: {
        activity: [
          { required: true, message: '请选择商户', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      goodsId: null,
      dataList: [], // 数据数组
      activityList: [], // 选择用数组
      isClick: false//
    }
  },
  watch: {
    dialogVisible(val) {
      this.isClick = false
      if (val) {
        getSupplierList({ page: 1, pageSize: 1000, endisable: 1 }).then(res => {
          // debugger
          this.dataList = res.data
          this.activityList = []
          for (const key in res.data) {
            this.activityList.push({
              id: key,
              label: res.data[key]['shopname']
            })
          }
          // if (res.activity_type) {
          //   this.form.activity = res.activity_type.split(',')
          // }
        })
      }
    }
  },
  methods: {
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const userData = []
          this.form.activity.forEach(element => {
            const d = {
              id: 0,
              member_id: 0
            }
            d['id'] = this.dataList[element]['id']
            d['member_id'] = this.dataList[element]['member_id']
            userData.push(d)
          })
          const params = {
            goods_id: this.goods['id'],
            users: userData
          }
          this.isClick = true
          batchCopyGoods(params).then(res => {
            this.dialogVisible = false
            this.$emit('getList', true)
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tip {
  line-height: 35px;
  margin-bottom: 10px;
}
</style>
